package com.mmlc.meimei.lichengkongjian.Adapter;

import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.WaitPayBean;
import com.mmlc.meimei.lichengkongjian.shoppingbag.ShoppingBagFragment;
import com.mmlc.network.filter.RequestOrderCheckedFilter;
import com.mmlc.network.filter.ShoppingCartListRequestFilter;
import com.mmlc.utils.PhoneUtils;
import com.mmlc.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.List;

public class WaitPayAdapter extends BaseAdapter {

    ViewGroup viewGroup;
    ViewGroup rootView;
    public WaitPayBean item;
    BaseParentActivity activity;
    ShoppingBagFragment fragment;
    private String[] idAndPosition;
    ShoppingCartListRequestFilter filter;


    public WaitPayAdapter(ShoppingBagFragment fragment,BaseParentActivity activity, ViewGroup viewGroup, WaitPayBean item, ViewGroup rootView) {
        this.viewGroup = viewGroup;
        this.rootView = rootView;
        this.item = item;
        this.activity = activity;
        this.fragment = fragment;
    }
    public WaitPayAdapter(ShoppingCartListRequestFilter filter,ShoppingBagFragment fragment, BaseParentActivity activity, ViewGroup viewGroup, WaitPayBean item, ViewGroup rootView) {
        this(fragment,activity,viewGroup,item,rootView);
        this.filter = filter;
    }

    @Override
    public int getCount() {
        return item.payBean.size();
    }

    @Override
    public WaitPayBean.PayBean getItem(int i) {
        return item.payBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void remove(int position){
        item.payBean.remove(position);
    }
    public void remove(List<WaitPayBean.PayBean> all){
        item.payBean.removeAll(all);
    }
    public boolean haveData( ){
        if(item.payBean.size() > 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int retV = 0;
        if(filter.couponNum > 0 && position == filter.couponNum){
            retV = 1;
        }
        return retV;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        int type = getItemViewType(i);
        if(type == 1){
            view = App.layoutInflater.inflate(R.layout.adapter_horizontal_line, viewGroup, false);
        }else {
            if (view == null) {
                view = App.layoutInflater.inflate(R.layout.adapter_gopay, viewGroup, false);
            }
        }

        TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);
        if(type == 1){//如果当前是无优惠券的Item
            tv_toast.setVisibility(View.VISIBLE);
        }else{
            tv_toast.setVisibility(View.GONE);
        }



        TextView tv_originalPrice = (TextView) view.findViewById(R.id.tv_originalPrice);
        if(App.user.getUserType() == 1){//内部员工
            tv_originalPrice.setVisibility(View.VISIBLE);
            tv_originalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            tv_originalPrice.setText("¥ "+item.payBean.get(i).market_price);
        }else{
            tv_originalPrice.setVisibility(View.GONE);
        }
        WaitPayBean.PayBean bean = item.payBean.get(i);
        final CheckBox cb_checked = (CheckBox) view.findViewById(R.id.cb_checked);
        cb_checked.setChecked(bean.isChecked);
        RelativeLayout rl_checkedContainer = (RelativeLayout) view.findViewById(R.id.rl_checkedContainer);
        cb_checked.setClickable(false);
        cb_checked.setFocusable(false);

        processChecked(rl_checkedContainer,cb_checked,i);


        ImageView iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
        x.image().bind(iv_photo,bean.photo);

        TextView tv_shopInfo = (TextView) view.findViewById(R.id.tv_shopInfo);
        tv_shopInfo.setText(bean.shopInfo);

        TextView tv_shopColor = (TextView) view.findViewById(R.id.tv_shopColor);
        tv_shopColor.setText(bean.shopColor);

        TextView tv_shopMoney = (TextView) view.findViewById(R.id.tv_shopMoney);
        tv_shopMoney.setText("¥ "+bean.shopPrice);

        TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
        tv_count.setText("x "+bean.shopCount);

        return view;
    }

    private void processChecked(RelativeLayout rl_checkedContainer, final CheckBox cb_checked, final int i) {
        rl_checkedContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WaitPayBean.PayBean bean = item.payBean.get(i);

                String rightText = fragment.tv_edit.getText().toString();
                if(rightText.equals("完成")){
                    if(bean.isChecked){
                        bean.isChecked = false;
                        cb_checked.setChecked(false);
                    }else{
                        bean.isChecked = true;
                        cb_checked.setChecked(true);
                    }
                    return;
                }

                int type = -1;
                if (bean.isChecked) {
                    type = 0;
                } else {
                    type = 1;
                }
                RequestOrderCheckedFilter filter = new RequestOrderCheckedFilter(activity);
                filter.offerErrorParams(rootView);
                filter.isTransparence = true;
                filter.requestBean.imei = PhoneUtils.getIMEI(activity);
                filter.requestBean.cid = bean.id;
                filter.requestBean.status = type;
                filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<String>() {
                    @Override
                    public void onSuccess(String result) {
                        WaitPayBean.PayBean bean = item.payBean.get(i);
                        if (bean.isChecked) {
                            bean.isChecked = false;
                            cb_checked.setChecked(false);
                        } else {
                            bean.isChecked = true;
                            cb_checked.setChecked(true);
                        }

                        if(u != null){
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                JSONObject o = jsonObject.getJSONObject("data");
                                String total_price = o.optString("total_price");
                                fragment.shopOriginal =  o.optString("total_price_market");
                                u.update(total_price);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }else {

                        }
                    }

                    @Override
                    public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                        ToastUtils.showToast(serverError.mess);
                    }
                });
            }
        });
    }

    public interface UpDatePrice{
        public void update(String totalPrice);
    }
    UpDatePrice u;
    public void setUpDatePrice(UpDatePrice u){
        this.u = u;
    }

    public String[] getSelecedIdAndPosition(){
        idAndPosition = new String[]{"",""};
        for(int j=0;j<item.payBean.size();j++){
            WaitPayBean.PayBean tmpValue = item.payBean.get(j);
            if(tmpValue.isChecked){
                idAndPosition[0] += tmpValue.id+",";
                idAndPosition[1] += j+",";
            }
        }

        if (!TextUtils.isEmpty(idAndPosition[0])) {
            idAndPosition[0] = idAndPosition[0].substring(0, idAndPosition[0].lastIndexOf(','));
            idAndPosition[1] = idAndPosition[1].substring(0, idAndPosition[1].lastIndexOf(','));
        }
        return idAndPosition;
    }
    public void resetSelecedIdAndPosition(){
        idAndPosition = new String[]{"",""};
    }
    public void selectedAll(){
        idAndPosition =   new String[]{"",""};
        for(int i=0;i<item.payBean.size();i++){
            WaitPayBean.PayBean payBean = item.payBean.get(i);

            idAndPosition[0] += payBean.id+",";
            idAndPosition[1] += i+",";
        }
    }
}